package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.CustomerVisit;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.CustomerVisitMapper;
import cn.wolfcode.car.business.query.CustomerVisitQuery;
import cn.wolfcode.car.business.service.ICustomerVisitService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageHelper;
import com.sun.mail.imap.protocol.ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CustomerVisitServiceImpl implements ICustomerVisitService {
    @Autowired
    private CustomerVisitMapper customerVisitMapper;


    @Override
    public TablePageInfo<CustomerVisit> query(CustomerVisitQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<CustomerVisit>(customerVisitMapper.selectForList(qo));
    }


    @Override
    public void save(CustomerVisit customerVisit) {
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
        String now = dateformat.format(date);
        //用户输入时间
        Date visitDate = customerVisit.getVisitDate();
        String visitTime = dateformat.format(visitDate);
        if (visitTime.compareTo(now) > 0) {
            throw new BusinessException("访问时间不能大于当前时间");
        }
        if (customerVisit.getCustomerId() == null
                ||customerVisit.getLinkmanId() == null
                ||customerVisit.getVisitType() == null
                ||customerVisit.getVisitReason() == null
                ||customerVisit.getVisitReason() == "".trim()
                ||customerVisit.getContent() == null
                ||customerVisit.getContent() == "".trim()
        ){
            throw new BusinessException("非法操作");
        }
        // 新增操作
        customerVisitMapper.insert(customerVisit);
    }

    @Override
    public CustomerVisit get(Long id) {
        return customerVisitMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(CustomerVisit customerVisit) {
        CustomerVisit dbItem = this.get(customerVisit.getId());

        customerVisitMapper.updateByPrimaryKey(customerVisit);
    }

    @Override
    public List<CustomerVisit> list() {
        return customerVisitMapper.selectAll();
    }

}
